//
//  DECXCategoryListViewController.m
//  DECXInStore
//
//  Created by 程强 on 15/12/9.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "DECXCategoryListViewController.h"
#import "DECXManager.h"
#import "DECXCategoryCell.h"
#import "DECXCategoryCollectionCell.h"
#import "DECXCategoryCollectionHeaderView.h"

@interface DECXCategoryListViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *categoryCollection;
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) NSArray *categoryAry;
@property (strong, nonatomic) NSArray *categoryChildAry;
@property (strong, nonatomic) NSString *pidCategoryName;
@end

@implementation DECXCategoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [DECXManager new];
    [self fetchCategory];
}
- (void)fetchCategory
{
    weak_Self(self);
    [self showHUD];
    [[self.manager category]subscribeNext:^(id x) {
        weakSelf.categoryAry = x;
        [weakSelf.pidCategoryTable reloadData];
        [weakSelf defaultSelectFirstRow];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        [weakSelf hideHUD];
    }];
}
- (void)defaultSelectFirstRow
{
    [self.pidCategoryTable reloadData];
    if (_categoryAry.count!=0) {
        DECXCategory *cat = _categoryAry[0];
        self.categoryChildAry =  cat.child;
        [self.categoryCollection reloadData];
        self.pidCategoryName = cat.category_name;
    }
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.pidCategoryTable selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.categoryAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"PidCell";
    DECXCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell == nil) {
        cell = [[DECXCategoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
    backView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView =backView;
    DECXCategory *pidCategory = self.categoryAry[indexPath.row];
    cell.categoryLbl.text = pidCategory.category_name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DECXCategory *pidCategory = self.categoryAry[indexPath.row];
    self.categoryChildAry = pidCategory.child;
    [self.categoryCollection reloadData];
    self.pidCategoryName = pidCategory.category_name;
}

#pragma mark --- UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.categoryChildAry.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    DECXCategory *cat = self.categoryChildAry[section];
    return cat.child.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"CategoryCell";
    DECXCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    DECXCategory *cat = _categoryChildAry[indexPath.section];
    DECXCategory *subCat = cat.child[indexPath.row];
    cell.categoryNameLbl.text = subCat.category_name;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DECXCategory *cat = _categoryChildAry[indexPath.section];
    DECXCategory *subCat = cat.child[indexPath.row];
    NSString *catName = [NSString stringWithFormat:@"%@-%@",cat.category_name,subCat.category_name];
    if (self.categoryBlock) {
        self.categoryBlock(subCat, catName);
        [self gotoBack];
    }
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        DECXCategoryCollectionHeaderView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CategoryHeader" forIndexPath:indexPath];
        //有缓存了
        DECXCategory *cat = _categoryChildAry[indexPath.section];
        header.categoryTitleLbl.text = cat.category_name;
        reusableView = header;
        [header categoryHeaderClickHandler:^{
            if (cat.child.count == 0) {
                NSString *catName = [NSString stringWithFormat:@"%@-%@",self.pidCategoryName,cat.category_name];
                if (self.categoryBlock) {
                    self.categoryBlock(cat, catName);
                    [self gotoBack];
                }
            }
        }];
    }
    return reusableView;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((ScreenWidth-130)/2, 41);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
